package com.binaryTree;

public class ConvertSortedArraytoBinarySearchTree0306_108 {
    public TreeNode sortedArrayToBST(int[] nums) {

        //左闭右开 [left,right)
        return sortedArrayToBST(nums, 0, nums.length);



    }

//    左闭右开 [left,right)
    private TreeNode sortedArrayToBST(int[] nums, int left, int right) {
        if(left >= right) return null;

        if(right - left == 1) return new TreeNode(nums[left]);

        int mid = left + (right - left) /2;
        TreeNode root = new TreeNode(nums[mid]);
        root.left = sortedArrayToBST(nums, left, mid);
        root.right = sortedArrayToBST(nums, mid+1, right);
        return root;
    }
}
